<template>
  <el-upload
    v-model:file-list="fileList"
    list-type="picture-card"
    :before-upload="handleBeforeUpload"
    :http-request="handleUpload"
    :on-remove="handleRemove"
    :on-preview="previewImg"
    :limit="props.limit"
  >
    <i-ep-plus/>
  </el-upload>

  <el-dialog v-model="dialogVisible" title="预览图">
    <img w-full :src="previewImgUrl" alt="Preview Image"/>
  </el-dialog>
</template>

<script setup lang="ts">
import {UploadFile, UploadProps, UploadRawFile, UploadRequestOptions, UploadUserFile,} from "element-plus";
import {uploadFileApi} from "@/api/file";
import defaultSettings from "@/settings";

const emit = defineEmits(["uploadMuliSuccess"]);
const props = defineProps({
  /**
   * 文件路径集合
   */
  modelValue: {
    type: Array < string >,
    default: () => [],
  },
  /**
   * 文件上传数量限制
   */
  limit: {
    type: Number,
    default: 10,
  },

  fkey: {
    type: String,
    default: "",
  },

});

const previewImgUrl = ref("");
const dialogVisible = ref(false);

const fileList = ref([] as UploadUserFile[]);
watch(
  () => props.modelValue,
  (newVal: string[]) => {
    const filePaths = fileList.value.map((file: any) => file.path);
    if (
      filePaths.length > 0 &&
      filePaths.length === newVal.length &&
      filePaths.every((x) => newVal.some((y) => y === x)) &&
      newVal.every((y) => filePaths.some((x) => x === y))
    ) {
      return;
    }
    fileList.value = newVal.map((filePath: any) => {
      if (!filePath.path)
        return {
          name: "",
        };

      return {
        id: filePath.picture_id || 0,
        url: filePath.path ? (defaultSettings.imgDomain + filePath.path) : '',
        name: filePath.name || "",
      } as UploadUserFile;
    });
  },
  {
    immediate: true
  });

/**
 * 自定义图片上传
 *
 * @param params
 */
async function handleUpload(options: UploadRequestOptions): Promise<any> {
  // 上传API调用
  const {data: fileInfo} = await uploadFileApi(options.file);

  // 上传成功需手动替换文件路径为远程URL，否则图片地址为预览地址 blob:http://
  const fileIndex = fileList.value.findIndex(
    (file) => file.uid == (options.file as any).uid
  );

  fileList.value.splice(fileIndex, 1, {
    id: parseInt(fileInfo.id),
    name: fileInfo.name,
    path: fileInfo.path,
    fkey: props.fkey,
    url: fileInfo.path ? (defaultSettings.imgDomain + fileInfo.path) : '',
  } as UploadUserFile);

  emit(
    "uploadMuliSuccess",
    fileList.value
    // fileList.value.map((file) => defaultSettings.imgDomain + file.url)
  );

}

/**
 * 删除图片
 */
function handleRemove(removeFile: UploadFile) {
  const filePath = removeFile.url;
  console.log(removeFile)

  if (filePath) {
    // 删除成功回调
    /* deleteFileApi(filePath).then(() => {
     });*/
    emit("uploadMuliSuccess", fileList.value);
  }
}

/**
 * 限制用户上传文件的格式和大小
 */
function handleBeforeUpload(file: UploadRawFile) {
  if (file.size > 2 * 1048 * 1048) {
    ElMessage.warning("上传图片不能大于2M");
    return false;
  }
  return true;
}

/**
 * 预览图片
 */
const previewImg: UploadProps["onPreview"] = (uploadFile) => {
  previewImgUrl.value = uploadFile.url!;
  if (!previewImgUrl.value)
    return;

  dialogVisible.value = true;
};
</script>
